package jmind.core.test;

import jmind.base.annotation.Ignore;
import jmind.base.util.bean.MapBeanUtil;
import jmind.core.support.MethodSupport;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.BeanUtilsBean2;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class A extends B {

    private int i;

    public String name;
    private Date date;


    public String getName() {
        return name;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    private int getO() {
        return i;
    }

    private void setO(int i) {
        this.i = i;
    }

    public Date getDate() {
        return date;
    }


    public void setName(String name) {
        this.name = name;
    }



    public static void main(String[] args) {
       Class clazz=A.class ;

        Field fillds = tryGetField(A.class,"id");
        System.err.println(fillds);
        Ignore annotation = fillds.getAnnotation(Ignore.class);
        System.err.println(annotation);


    }

    private static Field tryGetField(Class<?> clazz, String name) {
        if(clazz==null || Object.class.equals(clazz)){
            return null;
        }
        try {
            Field field = clazz.getDeclaredField(name);
           return  field;
        } catch (Exception e) {
            // ignore
            return tryGetField(clazz.getSuperclass(),name);
        }
    }

    public static List<Field> getFillds(Class clazz){
        List<Field> fields=new ArrayList<>();
        while (!Object.class.equals(clazz)){

            fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
            clazz=clazz.getSuperclass();
            System.err.println(clazz);
        }
        return  fields;
    }
    public  void testa(String[] args) {

        A a = new A();
     //   a.setDate(new Date());
        try {


            BeanUtils.setProperty(a, "name", "b");
            MethodSupport.setProperty(a, "o", 110);
            MethodSupport.setProperty(a, "i", 100);
            PropertyUtils.setProperty(a, "wa", "w");
      //      System.out.println(a.getName() + "--" + a.getO() + "---" + a.getWa());
            System.out.println(BeanUtils.getProperty(a, "name") + "----" + BeanUtils.getProperty(a, "o"));

            System.out.println(BeanUtils.getProperty(a, "date"));
            System.out.println(BeanUtilsBean.getInstance().getProperty(a, "date"));
            System.out.println(BeanUtilsBean.getInstance().getPropertyUtils().getProperty(a, "date"));
            System.out.println(BeanUtilsBean2.getInstance().getProperty(a, "date"));
            System.out.println(BeanUtilsBean2.getInstance().getPropertyUtils().getProperty(a, "date"));
            System.out.println(PropertyUtils.getProperty(a, "dategggggggg"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    void method() {
        A a = new A();
        Class claz = a.getClass();
        Field[] fields = claz.getDeclaredFields();
        for (Field field : fields) {
            try {
                //  field.setAccessible(true);
                Object value = field.getType().isPrimitive() ? 1 : "www";
                field.set(a, value);
                System.out.println(field.getName() + "----0-----" + field.getGenericType() + "-------"
                        + field.getType() + "----------" + field.get(a));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        System.out.println("*************1***********************************");

        fields = claz.getFields();
        for (Field field : fields) {
            System.out.println(field.getName() + "---1------" + field.getGenericType() + "-------" + field.getType());
        }
        System.out.println("***************2*********************************");
        Method[] methods = claz.getMethods();

        for (Method m : methods) {
            System.out.println(m.getName() + "----------" + m.getDefaultValue());
        }
        System.out.println("*******************3*****************************");
        methods = claz.getDeclaredMethods();
        for (Method m : methods) {
            System.out.println(m.getName() + "----------" + m.toGenericString());
        }
    }


}
